module hong {
	class config{
		/** 功能区高度 */
		static TOP:number = 89 - 30;
		/** 元素大小 */
		static SIZE:number = 67;
		/** Y最大数量 */
		static YMAX:number = 13;
		/** X最大数量 */
		static XMAX:number = 8;
		/** 边框 */
		static BORDER:number = 2;
	}
	/** 成语消消乐的主类 */
	export class cyxxgame extends baseGame {
		/** 赢面板 */
		private winView:eui.Group;
		/** 输面板 */
		private loseView:eui.Group;
		/** 等级显示标签 */
		private txtlv:eui.Label;
		/** 倒计时时间标签 */
		private txttime:eui.Label;
		/** 下一关按钮 */
		private winBt:eui.Button;
		/** 刷新按钮 */
		private loseBt:eui.Button;

		/** 成语列表 */
		private CYlist:any;
		/** 显示元素列表 */
		private list:cyxxItem[][] = null;
		/** 选中元素蒙版 */
		private selMask:egret.Shape[];
		/** 选中的元素 */
		private selItem:cyxxItem[];
		/** 选中的成语 */
		private selCY:string;
		/** 过去的秒数 */
		private sec:number = 0;
		/** 游戏类型 0:等级类型 1:年级类型第几册 */
		public ext:string = '';
		/** 当前选择的最高册 */
		public sjLevel:number = 0;
		/** 等级 */
		private Lv:number = 0;
		/** 留白高度 */
		private topHeight:number;

		public constructor() {
			super();
			this.RES = "cyxxl";
			this.list = [];
			this.selCY = "";
			this.skinName = "cyxxSkin";

			this.sec = 0;
			egret.setInterval(this.onTime, this, 1000);
			this.loseBt.addEventListener(egret.TouchEvent.TOUCH_TAP, this.gameShow, this);
			this.winBt.addEventListener(egret.TouchEvent.TOUCH_TAP, this.gameShow, this);
			
			this.box = new egret.DisplayObjectContainer();
			this.box.x = config.BORDER;
			this.box.y = config.TOP;
			this.box.width = this.width - config.BORDER;
			this.addChild(this.box);

			this.selMask = [];
			for(let i=0;i<4;i++){
				this.selMask[i] = new egret.Shape();
				this.selMask[i].graphics.beginFill(0xffffff,0.3);
				this.selMask[i].graphics.drawRect(0,0,config.SIZE,config.SIZE);
				this.selMask[i].graphics.endFill();
				this.selMask[i].visible = false;
			}
			this.selItem = [];

			//this.touchEnabled = true;
			this.box.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onClick, this);
		}
		/** 每秒刷新时间 */
		private onTime():void{
			if(this.sec > 0){
				if(this.winView.visible) return;
				this.sec--;
				this.txttime.text = this.sec.toString();

				if(this.sec == 0){
					this.box.visible = false;
					this.loseView.visible = true;
				}
			}
		}
		/** 游戏显示 */
		public gameShow():void{
			//this.top.texture = RES.getRes("top_png");
			this.loseView.visible = this.winView.visible = false;
			this.txtlv.text = (this.Lv + 1).toString();
			this.box.removeChildren();
			this.box.visible = true;
			this.CYlist || (this.CYlist = RES.getRes("cy_json"));
			let cy = "", cya = [], n = 0;
			this.sec = this.CYlist.lv[this.Lv][1];
			this.topHeight = (((config.YMAX - this.CYlist.lv[this.Lv][0]) / 2)|0) * config.SIZE;
			if(this.ext == ""){ //随便找的成语
				for(;cy.length<config.XMAX*this.CYlist.lv[this.Lv][0];){
					cy += this.CYlist.list[Math.random()*this.CYlist.list.length|0][0];
				}
			}else{ //苏教版小学成语
				this.sjLevel = parseInt(this.ext);
				if(this.sjLevel < 2) this.sjLevel = 2;
				let cyl = [];
				for(let i=1;i<=this.sjLevel;i++){
					if(i in this.CYlist.sj) cyl = cyl.concat(this.CYlist.sj[i]);
				}
				for(;cy.length<config.XMAX*this.CYlist.lv[this.Lv][0];){
					cy += cyl[Math.random()*cyl.length|0][0];
				}
			}
			cya = cy.split("");
			for(let i=0;i<this.CYlist.lv[this.Lv][0];i++){
				this.list[i] = [];
				for(let j=0;j<config.XMAX;j++){
					//if(cy.length == 0) cy = this.CYlist.list[Math.random()*this.CYlist.list.length|0];
					let cm:cyxxItem = new cyxxItem("T");
					let rd = Math.random() * (cya.length - n) | 0;
					cm.text = cya[rd];
					n++;
					cya[rd] = cya[cya.length - n];
					// if(Math.random()*100 < 50){
					// 	cm.text = cy.charAt(0);
					// 	cy = cy.substr(1);
					// }else{
					// 	cm.text = cy.charAt(cy.length-1);
					// 	cy = cy.substr(0,cy.length-1);
					// }
					this.list[i][j] = cm;
					cm.x = j * config.SIZE;
					cm.y = i * config.SIZE + this.topHeight;
					cm.touchEnabled = true;
					this.box.addChild(cm);
				}
			}
			for(let i=0;i<4;i++){
				this.box.addChild(this.selMask[i]);
				this.selMask[i].visible = false;
			} 
			this.selItem = [];
			this.selCY = "";
			super.gameShow();
		}
		/** 游戏的宽度 */
		//public gScaleX():number{
		//	return this.stage.stageWidth / (config.XMAX * config.SIZE);
		//}
		/** 游戏的高度 */
		//public gScaleY():number{
		//	return (this.stage.stageHeight - config.TOP) / (config.YMAX * config.SIZE);
		//}
		/** 点击了元素 */
		public onClick(e:egret.TouchEvent):void{ //console.log("click", e);
			let cItem:cyxxItem = e.target;
			let scy = this.selCY + cItem.text;
			if(this.ext == ""){ //
				for(let i=0;i<this.CYlist.list.length;i++){
					if(this.CYlist.list[i][0].indexOf(scy) == 0){
						if(scy.length == this.CYlist.list[i][0].length){ //成语选择成功
							for(let j=0;j<this.selItem.length;j++){
								this.list[(this.selItem[j].y - this.topHeight)/config.SIZE][this.selItem[j].x/config.SIZE] = null;
								this.box.removeChild(this.selItem[j]);
							}
							this.list[(cItem.y - this.topHeight)/config.SIZE][cItem.x/config.SIZE] = null;
							this.box.removeChild(cItem);
							this.reShow();
							this.selCY = "";
							this.selItem = [];
						}else{
							this.selItem.push(cItem);
							this.selCY += cItem.text;
							this.selMask[this.selItem.length-1].visible = true;
							this.selMask[this.selItem.length-1].x = cItem.x;
							this.selMask[this.selItem.length-1].y = cItem.y;
						}
						return;
					}
				}
			}else{ //苏教版成语
				for(let m=1;m<=this.sjLevel;m++){
					if(!(m in this.CYlist.sj)) continue;
					for(let i=0;i<this.CYlist.sj[m].length;i++){
						if(this.CYlist.sj[m][i][0].indexOf(scy) == 0){
							if(scy.length == this.CYlist.sj[m][i][0].length){ //成语选择成功
								for(let j=0;j<this.selItem.length;j++){
									this.list[(this.selItem[j].y - this.topHeight)/config.SIZE][this.selItem[j].x/config.SIZE] = null;
									this.box.removeChild(this.selItem[j]);
								}
								this.list[(cItem.y - this.topHeight)/config.SIZE][cItem.x/config.SIZE] = null;
								this.box.removeChild(cItem);
								this.reShow();
								this.selCY = "";
								this.selItem = [];
							}else{
								this.selItem.push(cItem);
								this.selCY += cItem.text;
								this.selMask[this.selItem.length-1].visible = true;
								this.selMask[this.selItem.length-1].x = cItem.x;
								this.selMask[this.selItem.length-1].y = cItem.y;
							}
							return;
						}
					}
				}
			}
			for(let i=1;i<this.selMask.length;i++){
				this.selMask[i].visible = false;
			}
			this.selItem = [cItem];
			this.selCY = cItem.text;
			this.selMask[this.selItem.length-1].visible = true;
			this.selMask[this.selItem.length-1].x = cItem.x;
			this.selMask[this.selItem.length-1].y = cItem.y;
		}
		/** 刷新显示的元素 */
		private reShow():void{
			// for(let i=0;i<this.Xnum;i++){
			// 	let cj = this.Ynum-1;
			// 	for(;cj>-1;cj--){ //取最后一个非空元素
			// 		if(this.list[cj][i] == null) break;
			// 	}
			// 	if(cj > -1) console.log("move", i, cj);
			// 	for(let j=cj-1;j>-1;j--){
			// 		if(this.list[j][i]){
			// 			this.list[cj][i] = this.list[j][i];
			// 			this.list[j][i] = null;
			// 			egret.Tween.get(this.list[cj][i]).to({y:cj*this.cellHeight}, 500);
			// 			cj--;
			// 		}
			// 	}
			// }
			if(this.box.numChildren == 4){
				this.winView.visible = true;
				this.Lv += 1;
				// let tx:egret.TextField = new egret.TextField();
				// tx.text = "你真棒赢了";
				// tx.size = 50;
				// tx.verticalAlign = egret.VerticalAlign.MIDDLE;
				// tx.textAlign = egret.HorizontalAlign.CENTER;
				// this.box.addChild(tx);
				// egret.setTimeout(function(){
				// 	this.Lv += 1;
				// 	this.gameShow();
				// }, this, 5000);
			}
			console.log("box.numChildren", this.box.numChildren);
			for(let i=0;i<this.selMask.length;i++){
				this.selMask[i].visible = false;
			}
		}
	}
}